package com.yan;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @author yanheng
 * @date 2020/6/9
 */
public class IdGenerator2 {

    AtomicLong id = new AtomicLong(0);

    private static IdGenerator2 instance;

    private IdGenerator2() {}

    public static IdGenerator2 getInstance() {
        if (instance == null) {
            synchronized (IdGenerator2.class) {
                if (instance == null) {
                    instance = new IdGenerator2();
                }
            }
        }
        return instance;
    }

    public long getId() {
        return id.incrementAndGet();
    }

    public static void main(String[] args) {
        System.out.println(IdGenerator2.getInstance().getId());
        System.out.println(IdGenerator2.getInstance().getId());
        System.out.println(IdGenerator2.getInstance().getId());

    }
}
